<template>
  <div id="box">
    <div class="header">
      <span class="left">
        <i class="icon font_family icon-zuojiantou"></i>
      </span>
      <span class="center">{{ articleData.title }}</span>
      <span class="right">2019-08-23</span>
    </div>

    <div class="content" v-html="articleData.content"></div>

    <div class="footer">
      <div class="title">
        <i class="icon font_family icon-goahead"></i>
        <span>相关资讯</span>
      </div>
      <div class="content">
        <router-link 
            class="link-title"
            tag="div" 
            v-for="item in articleData.related" :key="item._id" 
            :to="`/articles/${item._id}`">
            <!-- 应该是获取当前文章下的 2 个文章 -->
          {{ item.title }}
        </router-link>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'Article',
    props: {
      id: { required: true },
    },
    // 监听 id 的变化
    watch: {
      // 当 id 改变时， 执行 getArticleData 函数
      id: 'getArticleData'
    },
    data() {
      return {
        articleData: {
          id: '',
          title: '',
          content: '',
          createdAt: ''
        }
      }
    },
    methods: {
      async getArticleData() {
        const res = await this.$http.get(`/articles/${this.id}`);
        this.articleData = res.data
      }
    },
    created() {
      this.getArticleData();
    }
  }
</script>

<style lang="scss" scoped>
  .header {
    width: 100%;
    padding: 10px 5px;
    border-bottom: 1px solid #ccc;
    display: flex;
    background-color: #fff;
    .left {
      color: #409eff;
    }
    .center {
      padding-left: 10px;
      color: #409eff;
      display: inline-block;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .right {
      font-size: 12px;
      color: #ccc;
      float: right;
      white-space: nowrap;
    }
  }
  .footer {
    width: 100%;
    margin-top: 10px;
    padding: 15px 20px;
    background-color: #fff;
    .title {
      span {
        font-size: 18px;
        padding-left: 5px;
      }
    }
    .content {
      .link-title {
        font-size: 14px;
        line-height: 1.8rem;
        display: inline-block;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }
</style>
<style lang="scss">
  .content {
    background-color: #fff;
    padding: 5px 20px;
    p {
      line-height: 1em;
    }
    img {
      max-width: 100%;
      height: auto;
      border-radius: 5px;
    }
  }
</style>